package com.sxhuayuan.parking.controller.salesman;

import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Salesman;
import com.sxhuayuan.parking.service.SalesmanService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 业务员
 * 
 * @author helong
 * @create 2019-08-02 16:25
 */
@RestController("SalesmanController")
@RequestMapping("/salesman/salesman")
public class SalesmanController extends BaseController {

	@Resource(name = "salesmanServiceImpl")
	SalesmanService service;

	private static final String PATTERN = "^[^\\s&\"<>]+$";

	@PostMapping("/changepassword")
	public RespMessage changePassword(@RequestParam String password, @RequestParam String updatePassword) {
		Salesman current = service.getCurrent();
		if (current == null) {
			return invalidToken();
		}
		String s = DigestUtils.md5Hex(password);

		if (!s.equals(current.getPassword())) {
			return RespMessage.error("原密码输入错误");
		}
		if (!(updatePassword.length() >= 4 && updatePassword.length() <= 64 && updatePassword.matches(PATTERN))) {
			return RespMessage.error("新密码格式不正确");
		}
		current.setPassword(DigestUtils.md5Hex(updatePassword));
		service.update(current);
		return RespMessage.success();
	}

	@GetMapping("/getinfo")
	public RespMessage getinfo() {
		if (!service.isAuthenticated()) {
			return invalidToken();
		}
		String[] s = new String[1];
		return successResp(s);
	}

}
